home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-13 | 7.3 KB | 239 lines | [TEXT/CCL2] |
- ;;; -*- Mode: LISP; Package: CODE-WARRIOR; Syntax: Common-lisp; Base: 10; -*-
- ;;;
- ;;; Nov 28, Ray Pelletier, adapted to Code Warrior <pelletier@cmu.edu>
- ;;;
- ;;; Thr Nov 14 1991 by Guillaume Cartier <cartier@math.uqam.ca>
- ;;; think-c.lisp
- ;;;
- ;;;
- ;;; *****************************************************************
- ;;; General License Agreement and Lack of Warranty ******************
- ;;; *****************************************************************
- ;;;
- ;;; This software is distributed in the hope that it will be useful (both
- ;;; in and of itself), but WITHOUT ANY WARRANTY. The author does not accept
- ;;; responsibility to anyone for the consequences of using it or for whether
- ;;; it serves any particular purpose or works at all. No warranty is made
- ;;; about the software or its performance.
- ;;;
- ;;; The current version of this software may be obtained by anonymous ftp
- ;;; from cambridge.apple.com in the directory pub/MCL/CONTRIB.
- ;;;
- ;;; Please send bug reports, comments, questions and suggestions to
- ;;; cartier@math.uqam.ca. I would also appreciate receiving any changes
- ;;; or improvements you may make.
- ;;;
- ;;; *****************************************************************
- ;;; CW interface ************************************************
- ;;; *****************************************************************
- ;;;
- ;;; This interface consist of some lisp files and C header
- ;;; files, enabling one to easily use CodeWarrior functions in MCL. An
- ;;; example is also provided.
- ;;;
- ;;; Very special thanks to the MCL team, they have always been very
- ;;; generous of their time in responding promptly to any questions I had.
- ;;;
- ;;; *****************************************************************
- ;;; Revision History ************************************************
- ;;; *****************************************************************
- ;;;
- ;;; 25/01/91 - Posted the code for the first time at cambridge.
- ;;; 14/11/91 - Converted the code to MCL2.0b1.
- ;;; 10/12/94 - Modified to call CodeWarrior Code Resources - Ray Pelletier
- ;;;
-
-
- (require :ff)
- (provide :code-warrior)
-
- (defpackage "CODE-WARRIOR"
- (:use "COMMON-LISP" "CCL")
- (:import-from "CCL" "DEF-MACTYPE" "MAKE-MACTYPE" "%VREFLET")
- (:export "*CODE-WARRIOR-FOLDER*"
- "DEFCMODULE"
- "DEFAULT-RESOURCE-FILE"
- "LOAD-CMODULE"
- "CLOSE-CMODULE"
- ;;"%ALLOCATE-DOUBLE" ;;someone else can figure this out
- ;;"%MAKE-DOUBLE"
- ;;"%GET-DOUBLE"
- ;;"%PUT-DOUBLE"
- ))
-
- (in-package "CODE-WARRIOR")
-
-
- ;;; ***********************
- ;;; Global stuff **********
- ;;; ***********************
-
-
- (defvar *CODE-WARRIOR-FOLDER*
- "Alcatraz:CodeWarrior:")
-
- (defvar *CMODULE-RESOURCE-TYPE*
- "TCCD")
-
- (defvar *CMODULES-TABLE*
- (make-hash-table))
-
- (defvar *CMODULES*
- nil)
-
-
- (defstruct CMODULE
- name
- variables
- functions
- resource-file
- refnum)
-
-
- (defun GET-CMODULE (module-name)
- (or (gethash module-name *cmodules-table*)
- (error "Unknown C module ~a ." module-name)))
-
-
- ;;; *****************************
- ;;; CModule definition **********
- ;;; *****************************
-
-
- (defmacro DEFCMODULE (name &key variables functions
- (resource-file (default-resource-file name)))
- `(progn
- (defvar ,name)
- (setf (gethash ',name *cmodules-table*)
- (make-cmodule
- :name ',name
- :variables ',variables
- :functions ',(mapcar (function car) functions)
- :resource-file ,resource-file))
- (pushnew ',name *cmodules*)
- ,@(mapcar (function
- (lambda (symb)
- `(defvar ,symb)))
- variables)
- ,@(mapcan (function
- (lambda (spec)
- (apply (function expand-function-spec) name spec)))
- functions)
- ',name))
-
-
- (defun EXPAND-FUNCTION-SPEC (savedA4 symb argstype &optional restype)
- (let* ((args (loop for arg in argstype
- collect (if (keywordp arg)
- (copy-symbol arg)
- (intern (write-to-string arg)))))
- (lispargs (loop for x in argstype for y in args
- for type = (if (keywordp x) x (second x))
- when (eq type :lisp) collect y)))
- (list
- `(defvar ,symb)
- `(defun ,symb ,args
- (%vreflet ,(mapcar (function list) lispargs lispargs)
- (ff-call ,symb :a4 ,savedA4
- ,@(loop for x in (reverse args)
- collect :ptr collect x)
- ,(or restype :novalue)))))))
-
-
- (defun DEFAULT-RESOURCE-FILE (name)
- (merge-pathnames
- *code-warrior-folder*
- (symbol-name name)))
-
-
- ;;; *********************
- ;;; The loader **********
- ;;; *********************
-
-
- (defun LOADER-IMPORT (loader a4 symb)
- (with-pstrs ((str (symbol-name symb)))
- (let ((add (ff-call loader :a4 a4 :ptr str :a0)))
- (if (%null-ptr-p add)
- (error "Undefined C function ~a ." symb)
- (set symb add)))))
-
-
- (defun LOAD-CMODULE (module-name)
- (let ((module (get-cmodule module-name)))
- (setf (cmodule-refnum module)
- (open-resource-file (truename (cmodule-resource-file module))))
- (let ((res (get-resource *cmodule-resource-type* (symbol-name module-name))))
- (cond
- ((null res)
- (error "Can't find the C module ~a ." module-name))
- (t ;(#_DetachResource res)
- (let* ((loader (%get-ptr res))
- (a4 (ff-call loader :ptr (%null-ptr) :a0)))
- ;; Ask resource for A4 <==
- (set module-name a4)
- (dolist (symb (cmodule-variables module)) (loader-import loader a4 symb))
- (dolist (symb (cmodule-functions module)) (loader-import loader a4 symb))))))))
-
-
- (defun CLOSE-CMODULE (module-name)
- (let ((refnum (cmodule-refnum (get-cmodule module-name))))
- (unless (eq (#_CurResFile) refnum)
- (close-resource-file refnum))))
-
- (def-load-pointers RESTORE-CMODULES ()
- (dolist (cmodule *cmodules* t)
- (load-cmodule cmodule)))
-
- #|
- ;;; ********************************
- ;;; ******** CW's doubles **********
- ;;; ********************************
-
- ;; Must to be completed by someone who needs these...
-
- (defun (setf %GET-DOUBLE) (data pointer &optional (offset 0))
- (%put-double pointer data offset))
-
-
- (defun %ALLOCATE-DOUBLE ()
- (make-record :double))
-
- (defun %MAKE-DOUBLE (float)
- (let ((ptr (%allocate-double)))
- (setf (%get-double ptr) float)
- ptr))
-
- (defun %GET-DOUBLE (pointer &optional (offset 0))
- (let ((ptr (%inc-ptr pointer offset)))
- (ccl::%get-x2float ptr)))
-
- (defun %PUT-DOUBLE (pointer float &optional (offset 0))
- (let ((ptr (%inc-ptr pointer offset)))
- (ccl::%float2x (float float) ptr)))
- |#
- ;;
- ;; If you're using MCL2.0b3 or upwards, you can use the following
- ;; definition to ease working with doubles. In fact, you could probably
- ;; use it also in MCL2.0b1 with small changes (MCL2.0b1 does'nt recognize
- ;; the :access-operator keyword option to DEF-MACTYPE).
- ;;
-
- ;;; **************************
- ;;; Resources stuff **********
- ;;; **************************
-
-
- (defun OPEN-RESOURCE-FILE (file)
- (with-pstrs ((pf (mac-namestring (truename file))))
- (#_OpenResFile pf)))
-
- (defun CLOSE-RESOURCE-FILE (refnum)
- (#_CloseResFile refnum))
-
-
- (defun GET-RESOURCE (type name)
- (let ((res (with-pstrs ((ps name))
- (#_GetNamedResource type ps))))
- (unless (%null-ptr-p res) res)))
-